<?php
/**
 * Created by PhpStorm.
 * User: xuyiping
 * Date: 2019/1/25
 * Time: 17:43
 */

require __DIR__.'/config.php';

use PhpAmqpLib\Connection\AMQPStreamConnection;

$exchange = 'router';
$queue = 'msgs';

$consumerTag = 'consumer';

$connection = new AMQPStreamConnection(HOST,PORT,USER,PASSWORD,VHOST);
$channel = $connection->channel();


$channel->queue_declare($queue,false,true,false,false);

$channel->exchange_declare($exchange,'direct',false,true,false);

$channel->queue_bind($queue,$exchange);


function processMessage($message){

    echo "\n------------------\n";

    echo $message->body;
    echo "\n------------------\n";

    $message->delivery_info['channel']->basic_ack($message->delivery_info['delivery_tag']);

    if($message->body){
        $message->delivery_info['channel']->basc_cancel($message->delivery_info['consumer_tag']);
    }
}


$channel->basic_consume($queue,$consumerTag,false,false,false,false,'processMessage');

function shutDown($channel,$connection){

    $channel->close();
    $connection->close();
}

register_shutdown_function('shutDown',$channel,$connection);

while (count($channel->callbacks)){
    $channel->wait();
}